import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class WarehouseServer {
	public static void main(String[] helloworld) {
		try {
			Warehouse warehouse = new Warehouse();

			Properties env = new Properties();
		    env.put("java.naming.factory.initial","com.sun.jndi.cosnaming.CNCtxFactory");
		    env.put("java.naming.provider.url","iiop://localhost:2809");

		    Context ctx = new InitialContext(env);
		    ctx.rebind("WarehouseService", warehouse );

			Properties properties = new Properties();
			try {
				properties.load(new FileInputStream("products.properties"));
			} catch (IOException e) {
				e.printStackTrace();
			}
			Product product = null;
			Properties productList = new Properties();
			for (int i = 0; properties.getProperty(i + ".name") != null; i++) {
				product = new Product();
				product.setName(properties.getProperty(i + ".name"));
				product.setPrice(Float.parseFloat(properties.getProperty(i + ".price")));
				String category = properties.getProperty(i + ".category");
				product.setCategory(category);
				int amount = Integer.parseInt(properties.getProperty(i
						+ ".amount"));
				String firstTwo = category.substring(0, 2);
				product.setCode(firstTwo + i);
				productList.put(product, amount);
			}
			warehouse.setProductList(productList);
			System.out.println("[Aantal producten: " + productList.size() + "]");

		} catch (RemoteException ex) {
			System.out.println("Error" + ex.getMessage());
		
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 
}
